class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        s = list(s)
        size = len(s)

        if size < k:
            self.reverse(s, 0, size - 1)
        elif k <= size < (2 * k):
            self.reverse(s, 0, k - 1)
        else:
            # 余数
            remainder = size % (2 * k)
            div = size // (2 * k)
            # 这个是2k组
            for i in range(div):
                start = i * k
                end = (i + 1) * k - 1
                self.reverse(s, start, end)
            # 剩余组
            if remainder < k:
                self.reverse(s, 2 * k * div, size - 1)
            else:
                self.reverse(s, 2 * k * div, 2 * k * div+k-1)
        return "".join(s)

    def reverse(self, s: list, left, right):
        try:
            while left <= right:
                s[left], s[right] = s[right], s[left]
                left += 1
                right -= 1
        except:
            raise


# print(Solution().reverseStr("abcd", 3))
print(Solution().reverseStr("jlnnxsetgcpsbhsfymrkxfghdgcosnjssiysrufhubewllxzqhpasguvlrxtkgatzfybprfmmfithphckksnvjkcvnsqgsgosfxc", 20))
# print(Solution().reverseStr("abcdefg", 2))
